package com.jdz.gxq.info.admin.role.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.jdz.gxq.info.admin.role.bo.Menu;
import com.jdz.gxq.info.admin.role.form.MenuForm;
import com.jdz.gxq.info.admin.role.service.MenuService;
import com.jdz.gxq.info.common.action.CommonDispatchAction;
import com.jdz.gxq.info.common.bo.PageBean;

public class MenuAction extends CommonDispatchAction {

	private MenuService menuService = null;
	 
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		MenuForm menuForm = (MenuForm) form;

		String title = menuForm.getTitle();

		int totalCount = menuService.getMenusCount(title);

		int currentPage = menuForm.getPageIndex();

		PageBean pageBean = new PageBean(totalCount, currentPage);

		menuForm.setPageIndex(pageBean.getCurrentPage());

		Map<String, Object> params = new HashMap<String, Object>();

		// 分页用
		params.put("startRow", pageBean.getPageStartRow() - 1);
		params.put("rowSize", pageBean.getPageSize());
		params.put("title", title);

		List<Menu> menuList = menuService.getMenus(params);

		request.setAttribute("menuList", menuList);
		request.setAttribute("pageBean", pageBean);

		return mapping.findForward("success");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		MenuForm menuForm = (MenuForm) form;

		Menu menu = menuForm.getMenu();

		// menu名重复验证(其他后台逻辑验证也类似处理)
		if (!menuService.checkMenuName(menu, 1)) {
			ActionMessages messages = new ActionMessages();
			messages.add("menu.title", new ActionMessage(
					"error.menuTitleExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}

		menu.setModifiedBy(getUserId(request));

		menuService.insertMenu(menu);

		return mapping.findForward("success");
	}
	
	public ActionForward gotoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		MenuForm menuForm = (MenuForm) form;
		
		Menu menu = menuService.getMenuById(menuForm.getId());
		
		menuForm.setMenu(menu);

		return mapping.findForward("success");
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		MenuForm menuForm = (MenuForm) form;
		
		Menu menu = menuForm.getMenu();
		
		// menu名重复验证(其他后台逻辑验证也类似处理)
		if (!menuService.checkMenuName(menu, 2)) {
			ActionMessages messages = new ActionMessages();
			messages.add("menu.projectName", new ActionMessage("error.menuNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}
		
		menu.setModifiedBy(getUserId(request));
		
		menuService.updateMenu(menu);
		
		return mapping.findForward("success");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		String id = request.getParameter("id");
		
		String[] ids = id.split(",");
		
		for (String item : ids) {
			menuService.deleteMenu(Integer.parseInt(item));
		}

		return mapping.findForward("success");
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
}

